package com.sugar.subscribe.course.module.business.controller.app.coachcomment;

import com.sugar.subscribe.course.framework.common.pojo.CommonResult;
import com.sugar.subscribe.course.framework.common.pojo.PageParam;
import com.sugar.subscribe.course.framework.common.pojo.PageResult;
import com.sugar.subscribe.course.framework.idempotent.core.annotation.Idempotent;
import com.sugar.subscribe.course.framework.idempotent.core.keyresolver.impl.UserIdempotentKeyResolver;
import com.sugar.subscribe.course.framework.security.core.util.SecurityFrameworkUtils;
import com.sugar.subscribe.course.module.business.controller.app.coachcomment.vo.AppCoachCommentPageReqVO;
import com.sugar.subscribe.course.module.business.controller.app.coachcomment.vo.AppCoachCommentRespVO;
import com.sugar.subscribe.course.module.business.controller.app.coachcomment.vo.AppCoachCommentSaveReqVO;
import com.sugar.subscribe.course.module.business.service.coachcomment.CoachCommentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static com.sugar.subscribe.course.framework.common.pojo.CommonResult.success;

@Tag(name = "APP - 评论")
@RestController
@RequestMapping("/biz/coach-comment")
@Validated
public class AppCoachCommentController {

    @Resource
    private CoachCommentService coachCommentService;

    @PostMapping("/create")
    @Operation(summary = "用户端 - 创建评论")
    public CommonResult<Boolean> createCoachComment(@Valid @RequestBody AppCoachCommentSaveReqVO createReqVO) {
        coachCommentService.appCreateCoachComment(createReqVO, SecurityFrameworkUtils.getLoginUserId(false));
        return success(true);
    }

    @PostMapping("/like/{id}")
    @Operation(summary = "用户端 - 点赞/取消点赞评论")
    @Idempotent(message = "您的手速太快了！！！", keyResolver = UserIdempotentKeyResolver.class)
    public CommonResult<Boolean> likeComment(@PathVariable("id") Long id) {
        coachCommentService.likeComment(id, SecurityFrameworkUtils.getLoginUserId(false));
        return success(true);
    }

    @GetMapping("/delete")
    @Operation(summary = "用户端 - 删除评论")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteCoachComment(@RequestParam("id") Long id) {
        coachCommentService.appDeleteCoachComment(id, SecurityFrameworkUtils.getLoginUserId(false));
        return success(true);
    }

    @GetMapping("/page")
    @Operation(summary = "用户端 - 获得某位教练的评论分页")
    public CommonResult<PageResult<AppCoachCommentRespVO>> getCoachCommentPage(@Valid AppCoachCommentPageReqVO pageReqVO) {
        return success(coachCommentService.appGetCoachCommentPage(pageReqVO, SecurityFrameworkUtils.getLoginUserId(false)));
    }

    @GetMapping("/coach/page")
    @Operation(summary = "教练端 - 获得评论分页")
    public CommonResult<PageResult<AppCoachCommentRespVO>> appCoachGetCoachCommentPage(@Valid PageParam pageParam) {
        return success(coachCommentService.appCoachGetCoachCommentPage(pageParam, SecurityFrameworkUtils.getLoginUserId(true)));
    }
}